#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File Name: MetaData.py
# Author: bobo
# Created Time: 20241119
# pip install python-slugify

import frontmatter


def get_md_metadata(path: str):
    """获取markdown文件元数据字典"""
    with open(path, "r", encoding="utf-8") as f:
        content = f.read()

    pos = content.find("---", 5)
    if pos != -1:
        meta_data = content[:pos + 3]
        meta_data = meta_data.replace("<!", "", 1)
        # print(meta_data)
        post = frontmatter.loads(meta_data)
        if not post.metadata:  # 没有元数据
            # print(f"文件'{path}'没有元数据")
            return {}, ""

        content_pos = content.find("\n", pos)
        return post.metadata, content[content_pos + 1:]
    return {}, ""
